package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.content.ContentValues;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huopaolan.lib_core.Base.BaseActivity;
import com.huopaolan.yohuoshop.Adapter.CarListAdapter;
import com.huopaolan.yohuoshop.Bean.ShoppingTrolleyEntity;
import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.Di.Compont.DaggerShoppingTrolleyCompont;
import com.huopaolan.yohuoshop.Di.Modules.ShoppingTrolleyModules;
import com.huopaolan.yohuoshop.Mvp.Contract.ShoppingTrolleyContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.ShoppingTrolleyPresenter;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

public class ShoppingTrolleyActivity extends BaseActivity<ShoppingTrolleyPresenter> implements ShoppingTrolleyContract.ShoppingTrolleyIView, OnClickListener {
    private ImageView back_shop;
    private RecyclerView rv_car_list;
    private TextView tv_car_list_bj_wc, tv_car_list_sumprice, car_shop_num;
    private CarListAdapter carListAdapter;
    private LinearLayout ll_car_list_price, ll_car_list_save;
    private Button btn_car_list_account;
    private float SumPrice;
    private int ShopNum;
    private CheckBox cb_car_list_qx;
    private boolean flag = true;

    @Override
    public int layoutID() {
        return R.layout.activity_shopping_trolley;
    }

    @Override
    public void initView() {
        //初始化
        cb_car_list_qx = findViewById(R.id.cb_car_list_qx);
        car_shop_num = findViewById(R.id.car_shop_num);
        tv_car_list_sumprice = findViewById(R.id.tv_car_list_sumprice);
        ll_car_list_price = findViewById(R.id.ll_car_list_price);
        ll_car_list_save = findViewById(R.id.ll_car_list_save);
        rv_car_list = findViewById(R.id.rv_car_list);
        tv_car_list_bj_wc = findViewById(R.id.tv_car_list_bj_wc);
        back_shop = findViewById(R.id.iv_car_list_back);
        btn_car_list_account = findViewById(R.id.btn_car_list_account);
        cb_car_list_qx.setOnClickListener(this);
        btn_car_list_account.setOnClickListener(this);
        back_shop.setOnClickListener(this);
        tv_car_list_bj_wc.setOnClickListener(this);
        //添加适配器
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        carListAdapter = new CarListAdapter(R.layout.item_car_shop_layout);
        rv_car_list.setLayoutManager(manager);
        rv_car_list.setAdapter(carListAdapter);
    }

    @Override
    public void initData() {
        p.ShoppingTrolleyRequest();
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void setUpDagger() {
        DaggerShoppingTrolleyCompont.builder()
                .shoppingTrolleyModules(new ShoppingTrolleyModules(this))
                .build().inject(this);
    }

    @Override
    public void ShoppingTrolleySuccess(ShoppingTrolleyEntity entity) {
        if (entity == null) {
            return;
        }
        carListAdapter.addData(entity.values);
        String spReadUser = SpUtils.getInstance().SpReadUser();
        DBService.getDbService().delete("Shop", null, null);
        //存入数据库
        ContentValues values = new ContentValues();
        for (int i = 0; i < carListAdapter.getData().size(); i++) {
            values.put("userid", spReadUser);
            values.put("goodsid", entity.values.get(i).goods_id);
            values.put("car_path", entity.values.get(i).car_path);
            values.put("shop_name", entity.values.get(i).shop_name);
            values.put("shop_color", entity.values.get(i).shop_color);
            values.put("shop_size", entity.values.get(i).shop_size);
            values.put("shop_num", entity.values.get(i).shop_num);
            values.put("shop_price", entity.values.get(i).shop_price);
        }
        long user = DBService.getDbService().insert("Shop", values);
        if (user > 0) {
            Log.d("DBServiceShop", "数据库储存成功");
            DBService.getDbService().close();
        } else {
            Log.d("DBServiceShop", "数据库储存失败");
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_car_list_back:
                Toast.makeText(this, "点击", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.tv_car_list_bj_wc:
                if (flag) {
                    tv_car_list_bj_wc.setText("完成");
                    btn_car_list_account.setText("删除");
                    ll_car_list_price.setVisibility(View.GONE);
                    ll_car_list_save.setVisibility(View.VISIBLE);
                    flag = false;
                } else {
                    tv_car_list_bj_wc.setText("编辑");
                    btn_car_list_account.setText("结算");
                    ll_car_list_price.setVisibility(View.VISIBLE);
                    ll_car_list_save.setVisibility(View.GONE);
                    flag = true;
                }
                break;
            case R.id.btn_car_list_account:
                if (flag) {
                    for (int i = 0; i < carListAdapter.getData().size(); i++) {
                        if (carListAdapter.getData().get(i).flag == false) {
                            Toast.makeText(this, "没有选择", Toast.LENGTH_SHORT).show();
                            break;
                        } else {
                            carListAdapter.getData().get(i).flag = false;
                            if (carListAdapter.getData() == null) {
                                Toast.makeText(this, "没有商品", Toast.LENGTH_SHORT).show();
                                break;
                            } else {
                                Toast.makeText(this, "支付", Toast.LENGTH_SHORT).show();

                            }
                            cb_car_list_qx.setChecked(false);
                        }
                    }
                } else {
                    for (int i = 0; i < carListAdapter.getData().size(); i++) {
                        if (carListAdapter.getData().get(i).flag == false) {
                            Toast.makeText(this, "没有选择", Toast.LENGTH_SHORT).show();
                            break;
                        } else {
                            carListAdapter.getData().get(i).flag = false;
                            if (carListAdapter.getData() == null) {
                                Toast.makeText(this, "没有商品", Toast.LENGTH_SHORT).show();
                                break;
                            } else {
                                carListAdapter.getData().clear();
                            }
                            cb_car_list_qx.setChecked(false);
                        }
                    }
                }
                carListAdapter.notifyDataSetChanged();
                break;
            case R.id.cb_car_list_qx:
                for (int i = 0; i < carListAdapter.getData().size(); i++) {
                    int Shop_num = Integer.parseInt(carListAdapter.getData().get(i).shop_num);
                    float price_num = Float.parseFloat(carListAdapter.getData().get(i).shop_price);
                    if (carListAdapter.getData().get(i).flag == true) {
                        carListAdapter.getData().get(i).flag = false;
                        ShopNum = 0;
                        SumPrice = 00.0f;
                        tv_car_list_sumprice.setText("总计:￥" + SumPrice);
                        car_shop_num.setText("(" + ShopNum + "件" + ")");
                    } else {
                        carListAdapter.getData().get(i).flag = true;
                        SumPrice += price_num;
                        ShopNum += Shop_num;
                        tv_car_list_sumprice.setText("总计:￥" + SumPrice);
                        car_shop_num.setText("(" + ShopNum + "件" + ")");
                    }
                }
                carListAdapter.notifyDataSetChanged();
                break;
        }
    }
}
